package com.bls.cate.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bls.cate.R;
import com.bls.cate.constant.Constant;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/22.
 */

public class POMPopWindow extends PopupWindow implements View.OnClickListener {

    private ImageView ivAttention;
    private TextView tvAttention;
    private boolean currentAttentionStatus;
    private POMPopWinListener listener;
    private int windowWith;

    public POMPopWindow(Context context, POMPopWinListener listener) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        if (inflater != null) {
            view = inflater.inflate(R.layout.view_pop_person_other_more, null);
            this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            this.setContentView(view);
            this.setFocusable(true);
            this.setOutsideTouchable(true);
            this.update();
            ColorDrawable colorDrawable = new ColorDrawable(context.getResources().getColor(R.color.colorTrans));
            this.setBackgroundDrawable(colorDrawable);
            this.listener = listener;
            RelativeLayout btnAttention = view.findViewById(R.id.btn_attention);
            RelativeLayout btnChat = view.findViewById(R.id.btn_chat);
            RelativeLayout btnLetter = view.findViewById(R.id.btn_letter);
            ivAttention = view.findViewById(R.id.iv_attention);
            tvAttention = view.findViewById(R.id.tv_attention);
            btnAttention.setOnClickListener(this);
            btnChat.setOnClickListener(this);
            btnLetter.setOnClickListener(this);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnAttention.getLayoutParams();
            windowWith = layoutParams.width;
        }
    }

    /**
     * 设置关注状态
     *
     * @param isAttention 是否关注
     */
    public void setAttentionStatue(boolean isAttention) {
        ivAttention.setImageResource(isAttention ? R.mipmap.icon_attention_cancel : R.mipmap.icon_attention);
        tvAttention.setText(isAttention ? "取消关注" : "添加关注");
        currentAttentionStatus = isAttention;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_attention:
                listener.onPOMPopWinBtn(Constant.POMPopWinEnum.ATTENTION, currentAttentionStatus);
                break;
            case R.id.btn_chat:
                listener.onPOMPopWinBtn(Constant.POMPopWinEnum.CHAT, currentAttentionStatus);
                break;
            case R.id.btn_letter:
                listener.onPOMPopWinBtn(Constant.POMPopWinEnum.LETTER, currentAttentionStatus);
                break;

        }
    }

    public void showPopupWindow(View parent) {
        int screenWith = ScreenUtils.getScreenWidth();
        int xoff = screenWith - windowWith;
        if (!this.isShowing()) {
            this.showAsDropDown(parent, xoff, 0);
        } else {
            this.dismiss();
        }
    }

    public interface POMPopWinListener {
        void onPOMPopWinBtn(Constant.POMPopWinEnum pomPopWinEnum, boolean isAttention);
    }
}
